<template>
	<view :style="{ height: conentHeight + 'px;' }">
		<view class="top-height">
			<view class="page-top-box">
				<view class="search-box flex ac">
					<u-search
						borderColor="#DADADA"
						bgColor="#ffffff"
						placeholder="搜索项目名称"
						v-model="searchForm.project_name"
						:showAction="false"
						@search="searchFunc"
						@clear="searchFunc"
					></u-search>
					<view class="search-btn flex ac" @click="searchShow = true" :style="searchShow ? 'color:#3B75FF;' : ''">
						筛选
						<text class="iconfont icon-shaixuan" :style="searchShow ? 'color:#3B75FF;' : ''"></text>
					</view>
					<u-button
						@click="toAdd"
						v-if="$isPer(per_arr, ['afterSales_add'])"
						customStyle="width: auto;height: 64rpx;background: #3B75FF;border-radius: 32rpx;border-color:#F8F8FA;font-weight: 400;font-size: 26rpx;color: #ffffff;margin-left:20rpx;padding: 0 20rpx"
						:hairline="false"
						text="发起售后"
					></u-button>
				</view>
			</view>
		</view>
		<view class="list-content-box" :style="{ height: 'calc(100% - ' + topHeight + 'px);' }">
			<z-paging
				ref="paging"
				:fixed="false"
				v-model="dataList"
				@query="queryList"
				@onRefresh="refresh"
				:paging-style="{ 'background-color': '#f7f7f7', height: '100%', width: '100%' }"
			>
				<template slot="empty">
					<u-empty icon="https://hzybucket.obs.cn-south-1.myhuaweicloud.com/we_chat/no-msg2.png" text=" "></u-empty>
				</template>
				<view class="list-items flex ac flex-wrap" :key="index" v-for="(item, index) in dataList" @click.stop="navToDetailFunc(item, index)">
					<view class="list-items-title flex ac jb" v-if="$fieldShowFunc('sh_xmmc')">
						<view class="title u-line-1">{{ item.project_name }}</view>
						<u-tag color="#FFAC3B" borderColor="rgba(0,0,0,0)" bgColor="rgba(255, 172, 59, .15)" text="待处理" size="mini" v-if="item.after_sales_status == 10"></u-tag>
						<u-tag
							color="#3B75FF"
							borderColor="rgba(0,0,0,0)"
							bgColor="rgba(59, 117, 255, .15)"
							text="已完成"
							size="mini"
							v-else-if="item.after_sales_status == 60"
						></u-tag>
						<u-tag color="#34B325" borderColor="rgba(0,0,0,0)" bgColor="rgba(52, 179, 37, .15)" text="进行中" size="mini" v-else></u-tag>
					</view>
					<view class="list-items-item flex ac" v-if="$fieldShowFunc('sh_shgdbh')">
						<view class="title">工单编号</view>
						<view class="desc u-line-1">{{ item.after_sales_number }}</view>
					</view>
					<view class="list-items-item flex ac" v-if="$fieldShowFunc('sh_dqjd')">
						<view class="title">当前进度</view>
						<view class="desc u-line-1">
							<template v-if="item.after_sales_status == 10">待处理</template>
							<template v-else-if="item.after_sales_status == 60">已完成</template>
							<template v-else>进行中</template>
							（{{ speedDesc(3, item) }}）
						</view>
					</view>
					<view class="list-items-item flex ac" v-if="$fieldShowFunc('sh_shnr')">
						<view class="title">售后内容</view>
						<view class="desc u-line-1">{{ item.problem_content }}</view>
					</view>
					<view class="list-items-item flex ac" v-if="$fieldShowFunc('sh_cjsj')">
						<view class="title">创建时间</view>
						<view class="desc u-line-1">{{ item.created_at }}</view>
					</view>

					<!-- 按钮 -->
					<view style="width: 100%">
						<judgeBtn plain :per_arr="per_arr" :btns="btns" :state="item.after_sales_status" :item="item" :idx="index" @change="btnsChange"></judgeBtn>
					</view>
				</view>
			</z-paging>
		</view>
		<!-- 筛选弹窗 -->
		<u-popup :show="searchShow" round="30" @close="searchShow = false" closeable>
			<view class="search-popup-title">全部筛选</view>
			<scroll-view scroll-y class="search-popup-box">
				<view class="search-items">
					<view class="search-items-title flex ac jb">
						<text>工程监理</text>
						<view
							class="search-items-title-right flex ac jc"
							hover-class="hover-class"
							v-if="searchListObj.engineering_supervision_id.page != 1"
							@click="retractFunc('engineering_supervision_id')"
						>
							<text class="txt">收起</text>
							<u-icon name="arrow-up" color="#A1A7B3" size="12"></u-icon>
						</view>
					</view>
					<view class="search-items-list flex ac flex-wrap">
						<view
							@click="searchSelectFunc('engineering_supervision_id', item)"
							:class="searchForm.engineering_supervision_id == item.id ? 'search-items-list-item-active' : ''"
							class="search-items-list-item"
							hover-class="hover-class"
							v-for="(item, index) in searchListObj.engineering_supervision_id.list"
							:key="index"
						>
							{{ item.name }}
						</view>
						<view
							class="search-items-list-more flex ac jc"
							hover-class="hover-class"
							@click="lookMoreFunc('engineering_supervision_id')"
							v-if="searchListObj.engineering_supervision_id.list.length != searchListObj.engineering_supervision_id.max"
						>
							<text class="txt">查看更多</text>
							<u-icon name="arrow-down" color="#A1A7B3" size="12"></u-icon>
						</view>
					</view>
				</view>
				<view class="search-items">
					<view class="search-items-title flex ac jb">
						<text>当前进度</text>
					</view>
					<view class="search-items-list flex ac flex-wrap">
						<view
							@click="searchSelectFunc('after_status', item)"
							class="search-items-list-item"
							:class="item.value == searchForm.after_status ? 'search-items-list-item-active' : ''"
							hover-class="hover-class"
							v-for="(item, index) in searchListObj.after_status.list"
							:key="index"
						>
							{{ item.label }}
						</view>
					</view>
				</view>
				<view class="search-items">
					<view class="search-items-title flex ac jb">
						<text>维修工程经理</text>
						<view
							class="search-items-title-right flex ac jc"
							hover-class="hover-class"
							v-if="searchListObj.engineering_manager_id.page != 1"
							@click="retractFunc('engineering_manager_id')"
						>
							<text class="txt">收起</text>
							<u-icon name="arrow-up" color="#A1A7B3" size="12"></u-icon>
						</view>
					</view>
					<view class="search-items-list flex ac flex-wrap">
						<view
							@click="searchSelectFunc('engineering_manager_id', item)"
							:class="searchForm.engineering_manager_id == item.id ? 'search-items-list-item-active' : ''"
							class="search-items-list-item"
							hover-class="hover-class"
							v-for="(item, index) in searchListObj.engineering_manager_id.list"
							:key="index"
						>
							{{ item.name }}
						</view>
						<view
							class="search-items-list-more flex ac jc"
							hover-class="hover-class"
							@click="lookMoreFunc('engineering_manager_id')"
							v-if="searchListObj.engineering_manager_id.list.length != searchListObj.engineering_manager_id.max"
						>
							<text class="txt">查看更多</text>
							<u-icon name="arrow-down" color="#A1A7B3" size="12"></u-icon>
						</view>
					</view>
				</view>
				<view class="search-items">
					<view class="search-items-title flex ac jb">
						<text>售后责任判定</text>
						<view
							class="search-items-title-right flex ac jc"
							hover-class="hover-class"
							v-if="searchListObj.responsible_party_id.page != 1"
							@click="retractFunc('responsible_party_id')"
						>
							<text class="txt">收起</text>
							<u-icon name="arrow-up" color="#A1A7B3" size="12"></u-icon>
						</view>
					</view>
					<view class="search-items-list flex ac flex-wrap">
						<view
							@click="searchSelectFunc('responsible_party_id', item)"
							:class="searchForm.responsible_party_id == item.id ? 'search-items-list-item-active' : ''"
							class="search-items-list-item"
							hover-class="hover-class"
							v-for="(item, index) in searchListObj.responsible_party_id.list"
							:key="index"
						>
							{{ item.name }}
						</view>
						<view
							class="search-items-list-more flex ac jc"
							hover-class="hover-class"
							@click="lookMoreFunc('responsible_party_id')"
							v-if="searchListObj.responsible_party_id.list.length != searchListObj.responsible_party_id.max"
						>
							<text class="txt">查看更多</text>
							<u-icon name="arrow-down" color="#A1A7B3" size="12"></u-icon>
						</view>
					</view>
				</view>
				<view class="search-items">
					<view class="search-items-title flex ac jb">
						<text>售后类型划分</text>
						<view
							class="search-items-title-right flex ac jc"
							hover-class="hover-class"
							v-if="searchListObj.problem_type_id.page != 1"
							@click="retractFunc('problem_type_id')"
						>
							<text class="txt">收起</text>
							<u-icon name="arrow-up" color="#A1A7B3" size="12"></u-icon>
						</view>
					</view>
					<view class="search-items-list flex ac flex-wrap">
						<view
							class="search-items-list-item"
							@click="searchSelectFunc('problem_type_id', item)"
							:class="searchForm.problem_type_id == item.id ? 'search-items-list-item-active' : ''"
							hover-class="hover-class"
							v-for="(item, index) in searchListObj.problem_type_id.list"
							:key="index"
						>
							{{ item.name }}
						</view>
						<view
							class="search-items-list-more flex ac jc"
							hover-class="hover-class"
							@click="lookMoreFunc('problem_type_id')"
							v-if="searchListObj.problem_type_id.list.length != searchListObj.problem_type_id.max"
						>
							<text class="txt">查看更多</text>
							<u-icon name="arrow-down" color="#A1A7B3" size="12"></u-icon>
						</view>
					</view>
				</view>
				<view class="search-items">
					<view class="search-items-title">售后时效</view>
					<u--input placeholder="售后时效" border="surround" v-model="searchForm.after_sales_timeliness"></u--input>
				</view>
			</scroll-view>
			<view class="btn-box flex ac jb">
				<u-button
					@click="reSearchValFunc"
					text="重置"
					customStyle="width: 248rpx;height: 82rpx;background: #242834;border-radius: 12rpx;font-weight: 500;font-size: 30rpx;color: #FFFFFF;"
				></u-button>
				<u-button
					@click="searchFunc"
					text="查询"
					customStyle="width: 422rpx;height: 82rpx;background: #3B75FF;border-radius: 12rpx;font-weight: 500;font-size: 30rpx;color: #FFFFFF;"
				></u-button>
			</view>
		</u-popup>

		<afterSalesManagement :value.sync="per_name" :oper_item="oper_item" @submit="handleSubmit"></afterSalesManagement>
	</view>
</template>

<script>
import { handleSpeedDesc } from '@/utils/desc.js';
export default {
	name: 'afterSalesManagement',
	props: {
		conentHeight: {
			default: 0
		},
		option: {
			type: Object,
			default: {}
		}
	},
	data() {
		return {
			per_arr: [],
			btns: [
				{
					per_name: 'afterSales_completeContent',
					state: 10,
					name: '售后内容完善'
				},
				{
					per_name: 'afterSales_engineeringManager',
					state: 20,
					name: '分配工程经理'
				},
				{
					per_name: 'afterSales_feedback',
					state: 30,
					name: '现场反馈'
				},
				{
					per_name: 'afterSales_judge',
					state: 40,
					name: '责任判定'
				},
				{
					per_name: 'afterSales_successImg',
					state: 50,
					name: '上传完成照片'
				}
			],
			searchShow: false,
			dataList: [],
			searchForm: {
				project_name: '',
				after_status: '',
				engineering_supervision_id: '',
				engineering_manager_id: '',
				responsible_party_id: '',
				after_sales_timeliness: '',
				problem_type_id: ''
			},
			topHeight: 0,
			loading: false,
			searchListObj: {
				after_status: {
					list: [
						{
							label: '待处理',
							value: 1
						},
						{
							label: '处理中',
							value: 2
						},
						{
							label: '未完成',
							value: 4
						},
						{
							label: '已完成',
							value: 3
						}
					],
					page: 1,
					num: 5,
					max: 0
				},
				engineering_supervision_id: {
					list: [],
					page: 1,
					num: 5,
					max: 0
				},
				engineering_manager_id: {
					list: [],
					page: 1,
					num: 5,
					max: 0
				},
				responsible_party_id: {
					list: [],
					page: 1,
					num: 5,
					max: 0
				},
				problem_type_id: {
					list: [],
					page: 1,
					num: 5,
					max: 0
				}
			},
			oper_item: {},
			per_name: '',
			oper_idx: ''
		};
	},
	computed: {
		speedDesc() {
			return (type, item) => {
				return handleSpeedDesc(type, item, uni.getStorageSync('userInfo'));
			};
		}
	},
	mounted() {
		this.engineeringSupervisionList();
		this.getEngineeringManager();
		this.getResponsibleParty();
		this.getProblemType();
		this.getHeight();

		// 获取权限
		this.$Api
			.getHtmlAuthority({
				path: '/afterSalesManagement'
			})
			.then((res) => {
				console.log(res, 'getHtmlAuthority');
				if (res.code == 200) {
					this.per_arr = res.data.permission;
				}
			});
	},
	watch: {},
	methods: {
		refresh() {
			this.dataList = [];
		},
		// 重置弹窗筛选项
		reSearchValFunc() {
			this.searchForm = {
				project_name: '',
				after_status: '',
				engineering_supervision_id: '',
				engineering_manager_id: '',
				responsible_party_id: '',
				after_sales_timeliness: '',
				problem_type_id: ''
			};
			this.$nextTick(() => {
				this.searchFunc();
			});
		},
		// 刷新列表
		searchFunc() {
			this.$refs.paging.reload();
			this.searchShow = false;
		},
		getHeight() {
			// 创建节点查询
			let that = this;
			const systemInfo = uni.getSystemInfoSync();
			const query = uni.createSelectorQuery().in(that);
			query.select('.top-height').boundingClientRect();
			query.exec((res) => {
				that.topHeight = res[0].height + systemInfo.statusBarHeight + 20;
				console.log(that.topHeight, res[0].height);
				setTimeout(() => {
					that.loading = true;
				}, 0);
			});
		},
		// 去详情
		navToDetailFunc(item, index) {
			this.oper_idx = index;
			uni.navigateTo({
				url: '/pagesB/afterSalesManagementDetail/afterSalesManagementDetail?id=' + item.id
			});
		},
		queryList(page, num) {
			this.getAfterSalesList(page, num);
		},
		// 售后列表
		getAfterSalesList(page, num) {
			let searchForm = this.searchForm;
			uni.showLoading({
				mask: true
			});
			this.$Apipc
				.getAfterSalesList({
					page,
					num,
					project_name: searchForm.project_name,
					after_status: searchForm.after_status,
					engineering_supervision_id: searchForm.engineering_supervision_id,
					engineering_manager_id: searchForm.engineering_manager_id,
					responsible_party_id: searchForm.responsible_party_id,
					after_sales_timeliness: searchForm.after_sales_timeliness,
					problem_type_id: searchForm.problem_type_id
				})
				.then((res) => {
					uni.hideLoading();
					console.log(res, '售后列表');
					if (res.code == 200) {
						this.$refs.paging.complete(res.data.list);
					}
				});
		},
		// 弹窗筛选项
		searchSelectFunc(type, item) {
			switch (type) {
				case 'after_status':
					this.searchForm[type] = this.searchForm[type] == item.value ? '' : item.value;
					break;
				case 'engineering_supervision_id':
					this.searchForm[type] = this.searchForm[type] == item.id ? '' : item.id;
					break;
				case 'engineering_manager_id':
					this.searchForm[type] = this.searchForm[type] == item.id ? '' : item.id;
					break;
				case 'responsible_party_id':
					this.searchForm[type] = this.searchForm[type] == item.id ? '' : item.id;
					break;
				case 'problem_type_id':
					this.searchForm[type] = this.searchForm[type] == item.id ? '' : item.id;
					break;
				default:
			}
		},
		// 弹窗筛选项查看更多
		lookMoreFunc(type) {
			switch (type) {
				case 'engineering_supervision_id':
					this.searchListObj.engineering_supervision_id.page += 1;
					this.engineeringSupervisionList();
					break;
				case 'engineering_manager_id':
					this.searchListObj.engineering_manager_id.page += 1;
					this.getEngineeringManager();
					break;
				case 'responsible_party_id':
					this.searchListObj.responsible_party_id.page += 1;
					this.getResponsibleParty();
					break;
				case 'problem_type_id':
					this.searchListObj.problem_type_id.page += 1;
					this.getProblemType();
					break;
				default:
			}
		},
		// 弹窗筛选项列表收起
		retractFunc(type) {
			switch (type) {
				case 'engineering_supervision_id':
					this.searchListObj.engineering_supervision_id.page = 1;
					this.engineeringSupervisionList();
					break;
				case 'engineering_manager_id':
					this.searchListObj.engineering_manager_id.page = 1;
					this.getEngineeringManager();
					break;
				case 'responsible_party_id':
					this.searchListObj.responsible_party_id.page = 1;
					this.getResponsibleParty();
					break;
				case 'problem_type_id':
					this.searchListObj.problem_type_id.page = 1;
					this.getProblemType();
					break;
				default:
			}
		},
		// 工程监理
		engineeringSupervisionList() {
			this.$Apipc
				.getAdminList({
					page: this.searchListObj.engineering_supervision_id.page,
					num: this.searchListObj.engineering_supervision_id.num,
					identity: 1,
					position_id_arr: [8]
				})
				.then((res) => {
					console.log(res, '工程监理列表');
					if (res.code == 200) {
						if (this.searchListObj.engineering_supervision_id.page == 1) {
							this.searchListObj.engineering_supervision_id.list = [];
						}
						this.searchListObj.engineering_supervision_id.list = [...this.searchListObj.engineering_supervision_id.list, ...res.data.list];
						this.searchListObj.engineering_supervision_id.max = res.data.max;
					}
				});
		},
		// 维修工程经理
		getEngineeringManager() {
			var obj = {
				page: this.searchListObj.engineering_manager_id.page,
				num: this.searchListObj.engineering_manager_id.num,
				keyword: '',
				province_id: '',
				city_id: '',
				county_id: '',
				identity: 2,
				position_id_arr: [7]
			};
			this.$Apipc
				.getAdminList(obj)
				.then((res) => {
					console.log(res, '工程经理');
					if (res.code == 200) {
						if (this.searchListObj.engineering_manager_id.page == 1) {
							this.searchListObj.engineering_manager_id.list = [];
						}
						this.searchListObj.engineering_manager_id.list = [...this.searchListObj.engineering_manager_id.list, ...res.data.list];
						this.searchListObj.engineering_manager_id.max = res.data.max;
					}
				})
				.catch((err) => {});
		},
		// 获取责任判定列表
		getResponsibleParty() {
			this.$Apipc
				.getResponsibleParty({
					page: this.searchListObj.responsible_party_id.page,
					num: this.searchListObj.responsible_party_id.num
				})
				.then((res) => {
					console.log(res, '责任判定列表');
					if (res.code == 200) {
						if (this.searchListObj.responsible_party_id.page == 1) {
							this.searchListObj.responsible_party_id.list = [];
						}
						this.searchListObj.responsible_party_id.list = [...this.searchListObj.responsible_party_id.list, ...res.data.list];
						this.searchListObj.responsible_party_id.max = res.data.max;
					}
				});
		},
		// 售后类型列表
		getProblemType() {
			this.$Apipc
				.getProblemType({
					page: this.searchListObj.problem_type_id.page,
					num: this.searchListObj.problem_type_id.num
				})
				.then((res) => {
					console.log(res, '售后类型列表');
					if (res.code == 200) {
						if (this.searchListObj.problem_type_id.page == 1) {
							this.searchListObj.problem_type_id.list = [];
						}
						this.searchListObj.problem_type_id.list = [...this.searchListObj.problem_type_id.list, ...res.data.list];
						this.searchListObj.problem_type_id.max = res.data.max;
					}
				});
		},

		// 添加
		toAdd() {
			uni.navigateTo({
				url: '/pagesC/afterSalesManagement/add/add'
			});
		},
		// 操作按钮触发点击
		btnsChange(e) {
			console.log(e, 'xxxsww');

			this.oper_item = e.item;
			this.oper_idx = e.index;
			switch (e.per_name) {
				// 售后内容完善
				case 'afterSales_completeContent':
					this.per_name = e.per_name;
					break;
				// 分配工程经理
				case 'afterSales_engineeringManager':
					this.per_name = e.per_name;
					break;
				// 现场反馈
				case 'afterSales_feedback':
					this.per_name = e.per_name;
					break;
				// 责任判定
				case 'afterSales_judge':
					this.per_name = e.per_name;
					break;
				// 上传完成照片
				case 'afterSales_successImg':
					this.per_name = e.per_name;
					break;
			}
		},
		handleSubmit() {
			this.updateListIdx();
		},

		// 更新单条数据
		updateListIdx() {
			console.log('来了 老弟');
			let idx = this.oper_idx.toString();
			if (idx) {
				let item = this.dataList[idx];
				this.$Apipc
					.newAfterSalesDetails({
						after_sales_id: item.id
					})
					.then((res) => {
						console.log(res, '售后详情');
						if (res.code == 200) {
							let info = res.data;
							this.dataList[idx] = res.data;
							this.dataList.splice(0, 0);
						}
					});
			}
		}
	}
};
</script>

<style lang="scss"></style>
